package bb.lanxing.devices.sync;

import com.garmin.fit.ActivityMesg;
import com.garmin.fit.EventMesg;
import com.garmin.fit.RecordMesg;
import com.garmin.fit.SessionMesg;
import com.garmin.fit.ZonesTargetMesg;
import com.google.gson.Gson;

import bb.lanxing.App;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.calc.processer.ProcessorFactory;
import bb.lanxing.calc.processer.i.IWorkoutProcessor;
import bb.lanxing.device.sync.FitHelper;
import bb.lanxing.lib.devices.sync.FitTrans;
import bb.lanxing.model.EquipmentInfo;
import bb.lanxing.model.WorkoutDatabaseHelper;
import bb.lanxing.model.database.DeviceInfo;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.Enums;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DefaultFitProcessor extends BaseFitProcessor {
    private String address;
    private DeviceInfo deviceInfo;
    private String equipmentInfoJson;
    private int mSourceType;
    private IWorkoutProcessor workoutProcessor = ProcessorFactory.create(ProcessorFactory.TYPE_STATIC);//3
    private final List<RecordMesg> recordMesgList = new ArrayList();

    @Override
    protected void onActivityMesg(ActivityMesg activityMesg) {
    }

    @Override
    protected void onEventMesg(EventMesg eventMesg) {
    }

    @Override
    protected void onZonesTargetMesg(ZonesTargetMesg zonesTargetMesg) {
    }

    public DefaultFitProcessor(int i) {
        this.mSourceType = i;
    }

    public void setAddress(String str) {
        this.address = str;
    }

    @Override
    public void onStart(FitTrans fitTrans) {
        this.recordMesgList.clear();
        this.workoutProcessor.init();
        Workout workout = this.workoutProcessor.getWorkout();
        workout.setUploadStatus(Enums.UploadStatus.NotUpload);
        setWorkout(workout, fitTrans.getPath());
    }

    @Override
    public void onSport(SessionMesg sessionMesg) {
        FitHelper.sportTypeToWorkout(sessionMesg, this.workoutProcessor.getWorkout());
    }

    @Override
    public void onComplete(FitTrans fitTrans) {
        Workout workout = this.workoutProcessor.getWorkout();
        this.workoutProcessor.release();
        DeviceSyncHelper.syncWorkoutWithId(App.getContext(), workout.getId());
    }

    private void setWorkout(Workout workout, String str) {
        if (workout != null) {
            if (str != null) {
                workout.setFitPath(str);
            }
            String str2 = this.address;
            if (str2 != null) {
                DeviceInfo deviceInfo = this.deviceInfo;
                String str3 = "{}";
                if (deviceInfo == null) {
                    DeviceInfo deviceInfoToAddress = DeviceInfo.getDeviceInfoToAddress(str2);
                    this.deviceInfo = deviceInfoToAddress;
                    if (setEquipmentInfoJson(deviceInfoToAddress) != null) {
                        str3 = setEquipmentInfoJson(this.deviceInfo);
                    }
                    this.equipmentInfoJson = str3;
                } else if (!deviceInfo.getAddress().equals(this.address)) {
                    DeviceInfo deviceInfoToAddress2 = DeviceInfo.getDeviceInfoToAddress(this.address);
                    this.deviceInfo = deviceInfoToAddress2;
                    if (setEquipmentInfoJson(deviceInfoToAddress2) != null) {
                        str3 = setEquipmentInfoJson(this.deviceInfo);
                    }
                    this.equipmentInfoJson = str3;
                }
            }
            String str4 = this.equipmentInfoJson;
            if (str4 != null) {
                workout.setEquipmentInfo(str4);
            }
            WorkoutDatabaseHelper.save(workout);
        }
    }

    private String setEquipmentInfoJson(DeviceInfo deviceInfo) {
        if (deviceInfo != null) {
            EquipmentInfo equipmentInfo = new EquipmentInfo();
            equipmentInfo.setManufacturer(deviceInfo.getManufacturer());
            equipmentInfo.setModel(deviceInfo.getModel());
            equipmentInfo.setSn(deviceInfo.getSerial());
            equipmentInfo.setFw_rev(deviceInfo.getFirmwareVersion());
            equipmentInfo.setHw_rev(deviceInfo.getHardwareVersion());
            equipmentInfo.setSw_rev(deviceInfo.getSoftwareVersion());
            return new Gson().toJson(equipmentInfo);
        }
        return null;
    }

    public Workout getWorkout() {
        IWorkoutProcessor iWorkoutProcessor = this.workoutProcessor;
        if (iWorkoutProcessor != null) {
            return iWorkoutProcessor.getWorkout();
        }
        return null;
    }

    @Override
    protected void onSessionMesg(SessionMesg sessionMesg) {
        FitHelper.sessionMesgToWorkout(sessionMesg, this.workoutProcessor.getWorkout(), this.mSourceType);
    }

    @Override
    public void onRecordMesg(RecordMesg recordMesg) {
        SourcePoint mergeRecordMesgs;
        this.recordMesgList.add(recordMesg);
        Workout workout = this.workoutProcessor.getWorkout();
        if (((recordMesg.getPositionLong() != null && recordMesg.getPositionLat() != null) || workout.getSport() == Workout.TYPE_TRAINING) && (mergeRecordMesgs = mergeRecordMesgs(this.recordMesgList)) != null) {
            processSourcePoint(mergeRecordMesgs);
            this.recordMesgList.clear();
        }
        if (((recordMesg.getPositionLong() != null && recordMesg.getPositionLat() != null) || workout.getSport() == Workout.TYPE_INDOOR_BIKE) && (mergeRecordMesgs = mergeRecordMesgs(this.recordMesgList)) != null) {
            processSourcePoint(mergeRecordMesgs);
            this.recordMesgList.clear();
        }

        if (((recordMesg.getPositionLong() != null && recordMesg.getPositionLat() != null) || workout.getSport() == Workout.TYPE_BIKE_TRAINER) && (mergeRecordMesgs = mergeRecordMesgs(this.recordMesgList)) != null) {
            processSourcePoint(mergeRecordMesgs);
            this.recordMesgList.clear();
        }

        if (((recordMesg.getPositionLong() != null && recordMesg.getPositionLat() != null) || workout.getSport() == Workout.TYPE_ROWER) && (mergeRecordMesgs = mergeRecordMesgs(this.recordMesgList)) != null) {
            processSourcePoint(mergeRecordMesgs);
            this.recordMesgList.clear();
        }

        if (((recordMesg.getPositionLong() != null && recordMesg.getPositionLat() != null) || workout.getSport() == Workout.TYPE_CROSS_TRAINER) && (mergeRecordMesgs = mergeRecordMesgs(this.recordMesgList)) != null) {
            processSourcePoint(mergeRecordMesgs);
            this.recordMesgList.clear();
        }
        if (this.recordMesgList.size() > 100) {
            this.recordMesgList.clear();
        }
    }

    public void processSourcePoint(SourcePoint sourcePoint) {
        this.workoutProcessor.process(sourcePoint);
    }

    private SourcePoint mergeRecordMesgs(List<RecordMesg> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        Iterator<RecordMesg> it = list.iterator();
        RecordMesg recordMesg = null;
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            recordMesg = it.next();
            if (recordMesg.getPositionLong() != null && recordMesg.getPositionLat() != null) {
                it.remove();
                break;
            }
        }
        if (recordMesg == null) {
            return null;
        }
        for (RecordMesg recordMesg2 : list) {
            if (recordMesg2.getCadence() != null) {
                recordMesg.setCadence(recordMesg2.getCadence());
            }
            if (recordMesg2.getSpeed() != null) {
                recordMesg.setSpeed(recordMesg2.getSpeed());
            }
            if (recordMesg2.getHeartRate() != null) {
                recordMesg.setHeartRate(recordMesg2.getHeartRate());
            }
            if (recordMesg2.getPower() != null) {
                recordMesg.setPower(recordMesg2.getPower());
            }
        }
        return FitHelper.createSourcePointByFitMesg(recordMesg, this.mSourceType);
    }

    public int getSourceType() {
        return this.mSourceType;
    }
}
